import { observable, action } from 'mobx'
import axios from '../../../../common/request'
import notificationWithIcon from "../../../common/notificationWithIcon";
import modalDisplay from "../../../common/informModal";

class EnrollActivityStore {
    @observable page={
        currentPage:1,
        pageSize:15
    }
    @observable activities=[]
    @observable total
    @observable  _refreshFlag
    actions = {
        getActivity: action(()=>{
            axios.get('/api/activity/favorite/list?pageNumber='+this.page.currentPage+"&pageSize="+this.page.pageSize)
                .then((response)=> {
                    if(response.data.status.code===1){
                        Object.assign(this.activities,response.data.result.data)
                        this.total=response.data.result.total
                    }
                    else{
                        notificationWithIcon('error',response.data.status.message,"")
                    }
                })
                .catch(function (error) {
                    notificationWithIcon('error',"获取收藏列表失败",error.response)
                    console.log(error);
                });
        }),
        cancelStar: action((id) => {
            axios.post('/api/activity/favorite?favoriteId='+id)
                .then((response)=> {
                    if(response.data.status.code===1){
                        modalDisplay('success',"取消收藏成功","",null)
                        let index=this.activities.findIndex(item => item.id === id);
                        if(index!=-1)
                            this.activities.splice(index, 1)
                        this._refreshFlag = Math.random()
                    }
                    else{
                        modalDisplay('error',response.data.status.message,error.response,"",null)
                    }
                })
                .catch(function (error) {
                    modalDisplay('error',"取消收藏失败",error.response,null)
                    console.log(error);
                });
        }),
        setCurrentPage: action((page) => {
            this.page={
                currentPage:page,
                pageSize:this.page.pageSize
            }
            this.actions.getActivity()
        })
    }
}

export default new EnrollActivityStore()